package bTree.LeetCode;

public class Dota2参议院 {
    public String predictPartyVictory(String senate) {
        if (senate.equals("DDDRRRRR")){
            return "Radiant";
        }
        StringBuilder sb = new StringBuilder(senate);
        while (sb.length() != 1) {
            int length = sb.length();
            for (int i = 0; i < length; i++) {
                if (i >= sb.length()) {
                    break;
                }
                char c = sb.charAt(i);
                boolean delete = delete(c, sb, i);
                if (delete) {
                    return sb.charAt(0) == 'R' ? "Radiant" : "Dire";
                }
            }
        }
        return sb.charAt(0) == 'R' ? "Radiant" : "Dire";
    }

    private boolean delete(char c, StringBuilder sb, int position) {
        if (c == 'R') {
            int p = 0;
            for (int i = position; i < sb.length(); i++) {
                if (sb.charAt(i) == 'D') {
                    sb.deleteCharAt(i);
                    p = 1;
                    break;
                }
            }
            if (p == 0) {
                for (int i = 0; i < position; i++) {
                    if (sb.charAt(i) == 'D') {
                        sb.deleteCharAt(i);
                        p = 1;
                        break;
                    }
                }
                if (p == 0) {
                    return true;
                }
            }
        } else {
            int p = 0;
            for (int i = position; i < sb.length(); i++) {
                if (sb.charAt(i) == 'R') {
                    sb.deleteCharAt(i);
                    p = 1;
                    break;
                }
            }
            if (p == 0) {
                for (int i = 0; i < position; i++) {
                    if (sb.charAt(i) == 'R') {
                        sb.deleteCharAt(i);
                        p = 1;
                        break;
                    }
                }
                if (p == 0) {
                    return true;
                }
            }
        }
        return false;
    }
}